package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result courseList(String name,Integer subject,Integer target,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询参数:name:{},subject:{},target:{},page:{},pageSize:{}" , name, subject, target, page, pageSize);
        PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除课程id:{}" , id);
        courseService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("新增课程:{}" , course);
        return courseService.add(course);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询课程id:{}" , id);
         Course course = courseService.getById(id);
         return Result.success(course);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("更新课程:{}" , course);
        courseService.update(course);
        return Result.success();
    }
    //根据学科查询
    @GetMapping("/subject/{subject}")
    public Result subjectList(@PathVariable Integer subject){
        log.info("查询课程:{}" , subject);
         List<Course> course = courseService.subjectList(subject);
         return Result.success(course);
    }

    //查询所有课程
    @GetMapping("/list")
    public Result allList(String name, Integer subject, Integer target,
                          LocalDateTime createTime, LocalDateTime updateTime){
        log.info("查询参数:name:{},subject:{},target:{},createTime:{},updateTime:{}" , name, subject, target, createTime, updateTime);
        return courseService.allList(name, subject, target, createTime, updateTime);
    }
}
